﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;
using System.ComponentModel.Composition;
using Microsoft.Practices.ServiceLocation;

namespace Dynamic_UI_Framework
{
    [TemplatePart(Name = "PART_DragThumb", Type = typeof(DragThumb))]
    [TemplatePart(Name = "PART_ResizeDecorator", Type = typeof(Control))]
    [TemplatePart(Name = "PART_ContentPresenter", Type = typeof(ContentPresenter))]
    [Serializable]
    public class DesignerItemControl : ContentControl, ISelectable
    {
        public IVisual CurrentContent
        {
            get
            { return currentContent; }
            set
            {
                if (currentContent == value)
                    return;

                currentContent = value;
                this.Content = currentContent.Target;
            }
        }
        private IVisual currentContent;

        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }
        public static readonly DependencyProperty IsSelectedProperty =
          DependencyProperty.Register("IsSelected",
                                       typeof(bool),
                                       typeof(DesignerItemControl),
                                       new FrameworkPropertyMetadata(false));

        public bool IsInDesignMode
        {
            get { return true; }
        }
        public static readonly DependencyProperty IsInDesignModeProperty =
          DependencyProperty.Register("IsInDesignMode",
                                       typeof(bool),
                                       typeof(DesignerItemControl),
                                       new FrameworkPropertyMetadata(false));

        public static readonly DependencyProperty DragThumbTemplateProperty =
            DependencyProperty.RegisterAttached("DragThumbTemplate", typeof(ControlTemplate), typeof(DesignerItemControl));

        public static ControlTemplate GetDragThumbTemplate(UIElement element)
        {
            return (ControlTemplate)element.GetValue(DragThumbTemplateProperty);
        }
        public static void SetDragThumbTemplate(UIElement element, ControlTemplate value)
        {
            element.SetValue(DragThumbTemplateProperty, value);
        }

        public DesignerItemControl()
        {
            this.Loaded += new RoutedEventHandler(DesignerItem_Loaded);
        }
        void DesignerItem_Loaded(object sender, RoutedEventArgs e)
        {
            //if (DesignerContext.IsInDesignMode == false)
            //    return;

            //if (this.Content is IMButtonBase)
            //{
            //    this.ContextMenu.Items.Add((this.Content as IMButtonBase).GetCurrentMenuItem());
            //}

            if (base.Template != null)
            {
                ContentPresenter contentPresenter =
                    this.Template.FindName("PART_ContentPresenter", this) as ContentPresenter;
                if (contentPresenter != null)
                {
                    contentPresenter.IsHitTestVisible = false;
                    UIElement contentVisual = VisualTreeHelper.GetChild(contentPresenter, 0) as UIElement;
                    if (contentVisual != null)
                    {
                        DragThumb thumb = this.Template.FindName("PART_DragThumb", this) as DragThumb;
                        if (thumb != null)
                        {
                            ControlTemplate template =
                                DesignerItemControl.GetDragThumbTemplate(contentVisual) as ControlTemplate;
                            if (template != null)
                                thumb.Template = template;
                        }
                    }
                }
            }
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);

            //if (DesignerContext.IsInDesignMode == false)
            //    return;

            Canvas designer = VisualTreeHelper.GetParent(this) as Canvas;

            // update selection
            if (designer != null)
                if ((Keyboard.Modifiers & (ModifierKeys.Shift | ModifierKeys.Control)) != ModifierKeys.None)
                    if (this.IsSelected)
                    {
                        this.IsSelected = false;
                    }
                    else
                    {
                        this.IsSelected = true;
                    }
                else if (!this.IsSelected)
                {
                    foreach (var item in designer.Children)
                    {
                        (item as DesignerItemControl).IsSelected = false;
                    }
                    this.IsSelected = true;
                }
            e.Handled = false;
        }
    }
}
